package com.huawei.petSystemdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.hms.hwid.I;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.data.ShopRepository;
import com.huawei.petSystemdemo.ui.pet.Detail_page_Activity;
import com.huawei.petSystemdemo.utils.ImgRepository;
import com.huawei.petSystemdemo.utils.OnBindBottomListener;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.function.Consumer;

public class PetAdapter extends RecyclerView.Adapter<PetAdapter.PetViewHolder> {

    private OnBindBottomListener listener;
    List<Pets> petsList;
    List<Bitmap> bitmapList;
    Context context;

    public PetAdapter(List<Pets> petsList, Context context) {
        this.petsList = petsList;
        this.context = context;
    }

    public void setListener(OnBindBottomListener listener) {
        this.listener = listener;
    }

    public OnBindBottomListener getListener() {
        return listener;
    }

    public class PetViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        ImageView image;
        TextView pet_name;
        TextView pub_name;
        TextView pet_price;
        TextView shen;
        TextView city;
        public PetViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.pet_img);
            pet_name = itemView.findViewById(R.id.pet_name);
            pub_name = itemView.findViewById(R.id.pub_name);
            pet_price = itemView.findViewById(R.id.pet_price);
            shen = itemView.findViewById(R.id.shen);
            city = itemView.findViewById(R.id.city);
            //image = itemView.findViewById(R.id.pet_img);
        }
    }

    @NonNull
    @NotNull
    @Override
    public PetViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_pet, parent, false);
        return new PetViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull PetAdapter.PetViewHolder holder, int position) {
        Pets pets = petsList.get(position);

        ImgRepository.getImage("/pet/getImg?img=" + pets.getImg(), new Consumer<Bitmap>() {
            @Override
            public void accept(Bitmap bitmap) {
                holder.img.setImageBitmap(bitmap);
            }
        });
        //holder.img.setImageResource(R.drawable.cat);
        holder.pet_name.setText(pets.getName());
        holder.pet_price.setText(pets.getPrice() +"");

        ShopRepository.getByShopId(pets.getShopId(), new Consumer<Shop>() {
            @Override
            public void accept(Shop shop) {
                holder.pub_name.setText(shop.getName());
            }
        });



        if (position == petsList.size() - 1)
        {
           if (listener != null) listener.bind(petsList);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Detail_page_Activity.class);
                intent.putExtra("id", pets.getId());
                System.out.println("id:" + pets.getId());
                context.startActivity(intent);
            }
        });
    }



    @Override
    public int getItemCount() {
        return petsList.size();
    }
}
